import { post } from '../../../utils/request.js'
import global from '../../../utils/global.js'
let that
Page({
  data: {
    dataInfo:{}
  },
  onLoad: function (options) {
    that = this
    console.log(JSON.parse(options.dataInfo))
    that.setData({
      dataInfo: JSON.parse(options.dataInfo)
    })
  },
  // // 
  // goPay(){
  //   wx.navigateTo({
  //     url: '../paySuccess/paySuccess',
  //   })
  // },
    // 支付
  wechatAppletPay() {
    let order = {};
    let code = "";
    let payId = that.data.dataInfo.payId
    wx.login({
      success: function (res) {
        console.log(res,"2323")
        if (res.code) {
          //发起网络请求  
          console.log(res.code)
          code = res.code;
          console.log(code, "zaoqij")
          that.isPayInfo(code, payId, order)
        } else {
          wx.showModal({
            title: '提示',
            content: '获取用户信息失败！',
          })
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
    })
  },
  isPayInfo(code, payId, obj) {
    console.log(obj)
    let param = {
      code: code,
      payId: payId
    }
    post(global.apiUrl.wechatAppletPay, param, function (res) {
      console.log(res)
      that.wxPayOrder(res.obj, obj, payId)
    })
  },
  // 支付接口
  wxPayOrder: function (result, obj, payId) { //去微信支付-小程序
    let resultVal = result
    wx.requestPayment({
      "appId": resultVal.appId,
      "nonceStr": resultVal.nonceStr,
      "signType": resultVal.signType,
      "package": resultVal.pkg,
      "timeStamp": resultVal.timeStamp + '',//数值转化为字符串
      "paySign": resultVal.paySign,
      'success': function (res) {
        wx.hideLoading()
        wx.redirectTo({
          url: "/pages/pay/paySuccess/paySuccess?pay=1&order=" + JSON.stringify(obj) + "&payId=" + payId
        })

      },
      'fail': function (res) {
        console.log(res)
        wx.showModal({
          title: '提示',
          content: '支付失败',
          success: function () {
            wx.redirectTo({
              url: "/pages/pay/paySuccess/paySuccess?pay=0&order=" + JSON.stringify(obj) + "&payId=" + payId
            })
          },//接口调用成功
          fail: function () {
            wx.redirectTo({
              url: "/pages/pay/paySuccess/paySuccess?pay=0&order=" + JSON.stringify(obj) + "&payId=" + payId
            })
          },  //接口调用失败的回调函数  
        })


      },
      'complete': function (res) { }
    })
  },
})